package cn.wuxing.mediation.web.controller.infoconfig;

import cn.hutool.core.date.DateTime;
import cn.wuxing.mediation.common.annotation.Log;
import cn.wuxing.mediation.common.constant.UserConstants;
import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.core.domain.entity.IcMediatorCommittee;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.enums.BusinessType;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.infoconfig.domain.dto.AddMediatorCommitteeUserDTO;
import cn.wuxing.mediation.infoconfig.domain.dto.RemoveMediatorCommitteeUserDTO;
import cn.wuxing.mediation.infoconfig.domain.vo.MediatorCommitteeUserVO;
import cn.wuxing.mediation.infoconfig.service.IIcMediatorCommitteeService;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 调解委员会信息
 *
 * @author mediation
 */
@RestController
@RequestMapping("/infoconfig/mediatorcommittee")
public class IcMediatorCommitteeController extends BaseController {

    @Autowired
    private IIcMediatorCommitteeService icMediatorCommitteeService;

    /**
     * 根据id获取详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(this.icMediatorCommitteeService.selectCommitteeById(id));
    }

    /**
     * 新增调解委员会
     */
    @Log(title = "调解委员会管理", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult insert(@Validated @RequestBody IcMediatorCommittee icMediatorCommittee) {
        if (!this.icMediatorCommitteeService.checkCommitteeNameUnique(icMediatorCommittee)) {
            return error(
                    "新增调解委员会'" + icMediatorCommittee.getMediatorCommitteeName() + "'失败，调解委员会名称已存在");
        }
        icMediatorCommittee.setCreateBy(getUsername());
        return toAjax(this.icMediatorCommitteeService.insertMediatorCommittee(icMediatorCommittee));
    }

    /**
     * 修改调解委员会
     */
    @Log(title = "调解委员会管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody IcMediatorCommittee icMediatorCommittee) {
        Long id = icMediatorCommittee.getId();
        if (!this.icMediatorCommitteeService.checkCommitteeNameUnique(icMediatorCommittee)) {
            return error(
                    "修改调解委员会'" + icMediatorCommittee.getMediatorCommitteeName() + "'失败，调解委员会名称已存在");
        } else if (icMediatorCommittee.getParentId().equals(id)) {
            return error("修改调解委员会'" + icMediatorCommittee.getMediatorCommitteeName()
                    + "'失败，上级调解委员会不能是自己");
        } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, icMediatorCommittee.getStatus())
                && this.icMediatorCommitteeService.selectNormalChildrenById(id) > 0) {
            return error("该调解委员会包含未停用的子调解委员会！");
        }
        icMediatorCommittee.setUpdateBy(getUsername());
        icMediatorCommittee.setUpdateTime(DateTime.now());
        return toAjax(this.icMediatorCommitteeService.updateMediatorCommittee(icMediatorCommittee));
    }

    /**
     * 删除调解委员会
     */
    @Log(title = "调解委员会管理", businessType = BusinessType.DELETE)
    @PostMapping("/del/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (this.icMediatorCommitteeService.hasChildById(id)) {
            return warn("存在下级调解委员会,不允许删除");
        }
        AddMediatorCommitteeUserDTO dto = new AddMediatorCommitteeUserDTO();
        dto.setMediatorCommitteeId(id);
        if (this.icMediatorCommitteeService.checkMediatorCommitteeExistUser(dto)) {
            return warn("调解委员会存在用户,不允许删除");
        }
        return toAjax(this.icMediatorCommitteeService.deleteById(id));
    }

    /**
     * 调解委员会用户列表
     *
     * @param mediatorCommitteeId
     * @return
     */
    @GetMapping("/getMediatorCommitteeUserList")
    public TableDataInfo getMediatorCommitteeUserList(
            @NotNull(message = "mediatorCommitteeId不能为空") Long mediatorCommitteeId, String name) {
        startPage();
        List<MediatorCommitteeUserVO> list = this.icMediatorCommitteeService.getMediatorCommitteeUserList(name,
                mediatorCommitteeId);
        return getDataTable(list);
    }

    /**
     //     * 新增调解委员会用户
     //     */
    @Log(title = "调解委员会管理", businessType = BusinessType.INSERT)
    @PostMapping("/addMediatorCommitteeUser")
    public AjaxResult addWiaddMediatorCommitteeUserndowUser(@Validated @RequestBody AddMediatorCommitteeUserDTO dto) {
        if (this.icMediatorCommitteeService.checkMediatorCommitteeExistUser(dto)) {
            return error("该用户已存在");
        }
        this.icMediatorCommitteeService.addMediatorCommitteeUser(dto);
        return AjaxResult.success();
    }

    /**
     //     * 移除调解委员会用户
     //     */
    @Log(title = "调解委员会用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/removeMediatorCommitteeUser")
    public AjaxResult removeMediatorCommitteeUser(@Validated @RequestBody RemoveMediatorCommitteeUserDTO dto) {
        this.icMediatorCommitteeService.removeMediatorCommitteeUser(dto);
        return AjaxResult.success();
    }

    /**
     * 获取调解委员会树列表
     */
    @GetMapping("/mediatorCommitteeTree")
    public AjaxResult mediatorCommitteeTree(IcMediatorCommittee icMediatorCommittee) {
        return success(icMediatorCommitteeService.selectMediatorCommitteeTreeList(icMediatorCommittee));
    }

    /**
     * 获取调解委员会树列表
     */
    @GetMapping("/selectMediatorCommitteeTreeDropDown")
    public AjaxResult selectMediatorCommitteeTreeDropDown(IcMediatorCommittee icMediatorCommittee) {
        return success(icMediatorCommitteeService.selectMediatorCommitteeTreeDropDown(icMediatorCommittee));
    }
}
